package jcataloger.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.filechooser.FileSystemView;

import jcataloger.resources.Messages;
import jcataloger.util.Utils;

/**
 * This class is used to select a root file in the system to catalog and index it.<br>
 * It brings the system dependent root directories (i.e. in windows C, D, E ... in linux /, /mnt/hda1 ...)
 * @author sekobey
 *
 */
public class RootSelectionDialog extends JDialog implements ActionListener{
	private JButton buttonCancel;
	private File selection;
	private boolean cancelled;
	
	/**
	 * This is the default constructor
	 * @param owner
	 */
	public RootSelectionDialog(JFrame owner) {
		super( owner );
		
		Container c = getContentPane();
		c.add( getCenterPanel(), BorderLayout.CENTER );
		c.add( getSouthPanel(), BorderLayout.SOUTH );
		
		this.pack();
		this.setTitle(Messages.getString("RootSelectionDialog.title")); //$NON-NLS-1$
		this.setLocationRelativeTo(owner);
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		this.setModal(true);
		this.setVisible(false);
	}
	
	private JPanel getCenterPanel() {
		JPanel panel = new JPanel();
		FlowLayout f = new FlowLayout( FlowLayout.CENTER );
		panel.setLayout( f );
		FileSystemView fView = FileSystemView.getFileSystemView();
		File[] roots = File.listRoots();
		roots = Utils.getLinuxRoots(roots);
		JButton buttons[] = new JButton[ roots.length ];
		for ( int i=0; i<buttons.length; i++ ) {
			buttons[i] = new JButton( roots[i].toString(), fView.getSystemIcon(roots[i]));
			buttons[i].setSize(100,100);
			buttons[i].addActionListener( this );
			panel.add( buttons[i] );
		}
		
		return panel;
	}
	
	private JPanel getSouthPanel() {
		JPanel panel = new JPanel();
		FlowLayout f = new FlowLayout( FlowLayout.CENTER );
		panel.setLayout( f );
		buttonCancel = new JButton(Messages.getString("RootSelectionDialog.1")); //$NON-NLS-1$
		buttonCancel.addActionListener(this);
		panel.add( buttonCancel );
		
		return panel;
	}
	
	/**
	 * It returns the selected root directory.
	 * @return the selected root.
	 */
	public File getSelection() {
		return selection;
	}
	
	/**
	 * It returns if the user cancelled the job.
	 * @return
	 */
	public boolean isCancelled() {
		return cancelled;
	}

	public void actionPerformed(ActionEvent e) {
		if ( e.getSource() == buttonCancel ) {
			this.setVisible(false);
			cancelled = true;
		}
		else {
			cancelled = false;
			JButton btn = (JButton)e.getSource();
			File selected = new File( btn.getText() );
			if ( selected.list() == null ) {
				JOptionPane.showMessageDialog(this, Messages.getString("RootSelectionDialog.2"), Messages.getString("title.error"), JOptionPane.ERROR_MESSAGE);
				return;
			}
			selection = selected;
			this.setVisible( false );
		}
		
	}

}
